using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
   public class tbKhachhangDAL
    {
        private SqlConnection cn = null;
        public tbKhachhangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbKhachhang kh)
        {
            SqlCommand cm = new SqlCommand("sp_khachang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenkh", kh.Tenkh);
            cm.Parameters.AddWithValue("@email", kh.Email);
            cm.Parameters.AddWithValue("@sodienthoai", kh.Sodienthoai);
            cm.Parameters.AddWithValue("@diachi", kh.Diachi);
            cm.Parameters.AddWithValue("@matkhau", kh.Matkhau);
            cm.ExecuteNonQuery();
        }
        public void Update(tbKhachhang kh)
        {
            SqlCommand cm = new SqlCommand("sp_khachhang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh", kh.Makh);
            cm.Parameters.AddWithValue("@tenkh", kh.Tenkh);
            cm.Parameters.AddWithValue("@email", kh.Email);
            cm.Parameters.AddWithValue("@sodienthoai", kh.Sodienthoai);
            cm.Parameters.AddWithValue("@diachi", kh.Diachi);
            cm.Parameters.AddWithValue("@matkhau", kh.Matkhau);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_khachhang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madkh", iD);
            cm.ExecuteNonQuery();
        }
        public tbKhachhangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("khachhang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbKhachhangCollection khColl = new tbKhachhangCollection();

            while (dr.Read()) 
            {
                tbKhachhang kh = new tbKhachhang();
                kh.Makh = int.Parse(dr.GetValue(0).ToString());
                kh.Tenkh = dr.GetValue(1).ToString();
                kh.Email = dr.GetValue(2).ToString();
                kh.Sodienthoai = dr.GetValue(3).ToString();
                kh.Diachi = dr.GetValue(4).ToString();
                kh.Matkhau = dr.GetValue(5).ToString();
                khColl.Add(kh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khColl;
        }
    }
}
